# main.R

source('dados-inundacoes.R')
source('dados.R')
source('tablernd.R')
source('autointel.R')
source('capital-simulacao.R')
source('capital-fabrica.R')
source('capital-minmax.R')

SimulaGamma <- function(nanos, Danos)
{
	nfuracoes <- length(Danos)
	T <- rexpanos(nanos, nanos/nfuracoes)

	N <- 100 * (round(length(T)/100)+1)
	X <- rep(mean(Danos), N)
	j <- 1
	for(i in 1:length(distr)) {
		d <- distr[[i]]
		n <- round(N*d[2])
		j. <- j+n-1

		if(d[1] == 0)
			X[j:j.] <- rnorm(n, d[3], d[4])
		else if(d[1] == 1)
			X[j:j.] <- log(rlnorm(n, d[3], d[4]))
		else if(d[1] == 2)
			X[j:j.] <- rgamma(n, d[3], d[4])

		for(i in 1:length(X))  # negative nbrs -> zero
			X[i] <- max(X[i], 0)


		j <- j+n

		#di <- rndtable(1:length(distr), p, c(0, cumsum(p)))
		#d <- distr[[di]]
		#if(d[1] == 0)
		#	X[i] <- rnorm(1, d[3], d[4])
		#else if(d[1] == 1)
		#	X[i] <- log(rlnorm(1, d[3], d[4]))
		#else if(d[1] == 2)
		#	X[i] <- rgamma(1, d[3], d[4])
	}

	sample(X)
	X <- (X[1:length(T)]+.5) * breaks

	data.frame(T=T, X=X)
}

get_probs <- function(distr)
{
	p <- rep(0, length(distr))
	for(i in 1:length(distr))
		p[i] <- distr[[i]][2]
	p
}

histdados <- function(x,y)
	y / sum(y) / (x[2]-x[1])

Danos <- function(dummy)
{
	d <- c(30, 40, 45, 35, 20, 17, 21, 15)
	#d <- c(50, 30, 5, 20, 35, 40, 35, 20)
	#d <- c(30, 40, 50, 40, 41, 45, 30, 8)

	histdados(1:8, d)
}

main <- function()
{
	y <- Danos(0)
	h <- hist(y, nclass=8, plot=FALSE)
	y <- h$density

	breaks <<- h$breaks[2]
	distr <<- intelifit(1:length(y), y)[[2]]
	p <<- get_probs(distr)

	pause()
	#PlotCapitalPremio_RuinaFixa(0.01, 0.01, Danos(5), 60, 200)
	PlotPremioCapital_RuinaFixa(0.01, 0.01, Danos(5), .9, 2)



#	print(SimulaRuina_PremioFixo(500, Danos(5), 250, 4))
#	PlotCapitalPremio_RuinaFixa(0.1, 0.3, Danos(5), 60, 200)

#	PlotCapitalSimulado(5, 100, 1.3, 0)
}

